package city.spring.modules.bed.controller;

import city.spring.modules.bed.dto.BedRoomDTO;
import city.spring.modules.bed.entity.BedRoomEntity;
import city.spring.modules.bed.service.BedRoomService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：房间信息
 *
 * @author 侯坤林
 * @date 2020-04-26 12:01:45
 */
@RestController
@RequestMapping("/bed_room")
public class BedRoomController extends ApiController {
    private final BedRoomService bedRoomService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<BedRoomEntity, ?>> entityOrderFields;

    public BedRoomController(BedRoomService bedRoomService) {
        this.bedRoomService = bedRoomService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(BedRoomEntity::getId);
        entityOrderFields.add(BedRoomEntity::getBedFloorId);
        entityOrderFields.add(BedRoomEntity::getCode);
        entityOrderFields.add(BedRoomEntity::getName);
        entityOrderFields.add(BedRoomEntity::getDirection);
        entityOrderFields.add(BedRoomEntity::getGmtCreate);
        entityOrderFields.add(BedRoomEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>房间信息</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>房间信息</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<BedRoomEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>房间信息</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(bedRoomService.getById(id));
    }

    /**
     * 添加一个 <strong>房间信息</strong>
     *
     * @param dto 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody BedRoomDTO dto) {
        logger.info("修改数据： {}", dto);
        bedRoomService.saveBedRoom(dto);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(dto.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>房间信息</strong>
     *
     * @param id  主键ID
     * @param dto 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody BedRoomDTO dto) {
        dto.setId(id);
        bedRoomService.updateBedRoom(dto);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>房间信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        bedRoomService.deleteBedRoom(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>房间信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        bedRoomService.deleteBedRoom(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<BedRoomEntity> buildLambdaQuery(BedRoomEntity entity) {
        LambdaQueryChainWrapper<BedRoomEntity> lambdaQuery = bedRoomService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), BedRoomEntity::getId, entity.getId());
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getBedFloorId()), BedRoomEntity::getBedFloorId, entity.getBedFloorId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCode()), BedRoomEntity::getCode, entity.getCode());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), BedRoomEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDirection()), BedRoomEntity::getDirection, entity.getDirection());
        lambdaQuery.like(entity.getGmtCreate() != null, BedRoomEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.like(entity.getGmtModified() != null, BedRoomEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

}
